
package com.jf.cloud.platform.controller;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.PhoneUtil;
import com.jf.cloud.api.auth.bo.UserInfoInTokenBO;
import com.jf.cloud.api.auth.constant.SysTypeEnum;
import com.jf.cloud.api.auth.feign.AccountFeignClient;
import com.jf.cloud.api.auth.vo.AuthAccountVO;
import com.jf.cloud.api.platform.vo.SysUserVO;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.platform.dto.SysUserDTO;
import com.jf.cloud.platform.model.SysUser;
import com.jf.cloud.platform.service.SysUserService;
import com.jf.cloud.platform.vo.SysUserSimpleVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author zz
 * @date 2020/12/21
 */
@RequestMapping(value = "/p/sys_user")
@RestController
@Tag(name = "平台用户信息")
public class SysUserController {

    @Value("${jfcloud.expose.operation.auth:}")
    private Boolean permission;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private AccountFeignClient accountFeignClient;

    @GetMapping("/info")
    @Operation(summary = "登陆平台用户信息" , description = "获取当前登陆平台用户的用户信息")
    public ServerResponseEntity<SysUserSimpleVO> info() {
        UserInfoInTokenBO userInfoInTokenBO = AuthUserContext.get();
        SysUserSimpleVO sysUserSimple = sysUserService.getSimpleByUserId(userInfoInTokenBO.getUserId());
        sysUserSimple.setIsAdmin(userInfoInTokenBO.getIsAdmin());
        sysUserSimple.setUserId(userInfoInTokenBO.getUserId());
        sysUserSimple.setUserName(userInfoInTokenBO.getUserName());
        return ServerResponseEntity.success(sysUserSimple);
    }

    @GetMapping("/page")
    @Operation(summary = "平台用户列表" , description = "获取平台用户列表")
    public ServerResponseEntity<PageVO<SysUserVO>> page(@Valid PageDTO pageDTO, @ParameterObject SysUserDTO sysUserDTO) {
        sysUserDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<SysUserVO> sysUserPage = sysUserService.pageByShopId(pageDTO, sysUserDTO);
        if (BooleanUtil.isFalse(permission)) {
            for (SysUserVO sysUserVO : sysUserPage.getList()) {
                sysUserVO.setPhoneNum(Objects.isNull(sysUserVO.getPhoneNum()) ? null : PhoneUtil.hideBetween(sysUserVO.getPhoneNum()).toString());
            }
        }
        return ServerResponseEntity.success(sysUserPage);
    }

    @GetMapping
    @Operation(summary = "获取平台用户信息" , description = "根据用户id获取平台用户信息")
    public ServerResponseEntity<SysUserVO> detail(@RequestParam Long sysUserId) {
        SysUserVO sysUserVO = sysUserService.getByUserId(sysUserId);
        if (BooleanUtil.isFalse(permission)) {
            sysUserVO.setPhoneNum(Objects.isNull(sysUserVO.getPhoneNum()) ? null : PhoneUtil.hideBetween(sysUserVO.getPhoneNum()).toString());
        }
        ServerResponseEntity<AuthAccountVO> authResponseEntity = accountFeignClient.getByUserIdAndType(sysUserVO.getSysUserId(), SysTypeEnum.PLATFORM.value());
        if (authResponseEntity.isFail()) {
            throw new LuckException(authResponseEntity.getMsg());
        }
        sysUserVO.setIsAdmin(authResponseEntity.getData().getIsAdmin());
        return ServerResponseEntity.success(sysUserVO);
    }

    @PostMapping
    @Operation(summary = "保存平台用户信息" , description = "保存平台用户信息")
    public ServerResponseEntity<Void> save(@Valid @RequestBody SysUserDTO sysUserDTO) {
        SysUser sysUser = mapperFacade.map(sysUserDTO, SysUser.class);
        sysUser.setSysUserId(null);
        sysUser.setHasAccount(0);
        List<Long> roleIds = sysUserDTO.getRoleIds();
        sysUserService.save(sysUser, roleIds);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新平台用户信息" , description = "更新平台用户信息")
    public ServerResponseEntity<Void> update(@Valid @RequestBody SysUserDTO sysUserDTO) {
        SysUser sysUser = mapperFacade.map(sysUserDTO, SysUser.class);
        sysUserService.update(sysUser, sysUserDTO.getRoleIds());
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除平台用户信息" , description = "根据平台用户id删除平台用户信息")
    public ServerResponseEntity<Void> delete(@RequestParam Long sysUserId) {
        if (Objects.equals(AuthUserContext.get().getUserId(), sysUserId)) {
            throw new LuckException("您正在使用当前账号，无法进行删除操作");
        }
        sysUserService.deleteById(sysUserId);
        return ServerResponseEntity.success();
    }
}
